package ee.maoinvest.frontend.controller.command.enterpriseCommands;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.Enterprise;
import ee.maoinvest.backend.model.data.views.EnterpriseContractWithElectricityMeter;
import ee.maoinvest.backend.services.EnterpriseRelationsWithElectricityMetersAndContractsService;
import ee.maoinvest.backend.services.EnterpriseService;
import ee.maoinvest.backend.services.implementations.EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl;
import ee.maoinvest.backend.services.implementations.EnterpriseServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriseforms.DeleteEnterpriseFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.EnterpriseFormsRequestProcessor;

public class FillDeleteEnterpriseFormConverterWithInitialDataCommand implements Command
{
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	private final int ID_PARSE_ERROR_IDENTIFICATOR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		EnterpriseFormsRequestProcessor enterpriseFormsRequestProcessor = 
				new EnterpriseFormsRequestProcessor();
		DeleteEnterpriseFormConverter deleteEnterpriseFormConverter = null;
		List<EnterpriseContractWithElectricityMeter> listOfAllEnterpriseContractsWithElectricityMeters = null;
		EnterpriseRelationsWithElectricityMetersAndContractsService enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = null;
		EnterpriseService enterpriseServiceImpl = null;
		Enterprise enterpriseBeingDeleted = null;
		int enterpriseIDToDelete = enterpriseFormsRequestProcessor.getEnterpriseIDToDelete(request);
		
		
		if (enterpriseIDToDelete != ID_PARSE_ERROR_IDENTIFICATOR)
		{
			enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = 
					new EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl();
			deleteEnterpriseFormConverter = new DeleteEnterpriseFormConverter();
			enterpriseServiceImpl = new EnterpriseServiceImpl();
			enterpriseBeingDeleted = enterpriseServiceImpl.getEnterpriseById(enterpriseIDToDelete);
			listOfAllEnterpriseContractsWithElectricityMeters = 
					enterpriseRelationsWithElectricityMetersAndContractsServiceImpl.getAllEnterpriseContractsWithElectricityMeters(
							enterpriseIDToDelete);
			deleteEnterpriseFormConverter.fillWithInitialData(enterpriseBeingDeleted, 
					listOfAllEnterpriseContractsWithElectricityMeters);
			request.setAttribute("deleteEnterpriseFormConverter", deleteEnterpriseFormConverter);
			
			HttpSession session = request.getSession();
			session.setAttribute("enterpriseBeingDeleted", enterpriseBeingDeleted);
			session.setAttribute("listOfAllEnterpriseContractsWithElectricityMeters", listOfAllEnterpriseContractsWithElectricityMeters);
			operationResult = OPERATION_SUCCESSFUL;	
		}
		else
		{
			operationResult = OPERATION_UNSUCCESSFUL;
		}
		
		return operationResult;
	}
}
